package com.vn.rainbow.exoalarm;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.github.clans.fab.FloatingActionButton;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nineoldandroids.view.ViewHelper;
import com.vn.rainbow.exoalarm.fragment.BaseFragment;
import com.vn.rainbow.exoalarm.fragment.ListAlarmFragment;
import com.vn.rainbow.exoalarm.fragment.SetAlarmFragment;
import com.vn.rainbow.exoalarm.receiver.TickReceiver;
import com.vn.rainbow.exoalarm.ui.TimeView;

public class MainActivity extends ActionBarActivity {

	public static final int INDEX_LIST_ALARM = 1;
	public static final int INDEX_SET_ALARM = 2;
	private int indexFragment;

	FloatingActionButton fab;
	BaseFragment mListAlarmFragment, mSetAlarmFragment;

	private boolean doubleBackToExitPressedOnce;

	private AnimatorSet mAnimatorSet;

	private boolean changeIcon;

	private TimeView mTimeView;

	private TickReceiver timeReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {

			mListAlarmFragment = new ListAlarmFragment();
			mSetAlarmFragment = new SetAlarmFragment();

			getSupportFragmentManager().beginTransaction()
					.add(R.id.content, mListAlarmFragment).commit();

			indexFragment = INDEX_LIST_ALARM;
		}

		mTimeView = (TimeView) findViewById(R.id.time_view);
		timeReceiver = new TickReceiver();
		registerReceiver(timeReceiver,
				new IntentFilter(Intent.ACTION_TIME_TICK));
		timeReceiver.addListener(mTimeView);

		fab = (FloatingActionButton) findViewById(R.id.fab);
		fab.hide(false);

		fab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				switch (indexFragment) {
				case INDEX_LIST_ALARM:
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.content, mSetAlarmFragment).commit();

					indexFragment = INDEX_SET_ALARM;

					mAnimatorSet.start();

					changeIcon = true;
					break;

				case INDEX_SET_ALARM:
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.content, mListAlarmFragment).commit();
					indexFragment = INDEX_LIST_ALARM;
					// Toast.makeText(getApplicationContext(), "Save", 1000)
					// .show();

					mAnimatorSet.start();
					changeIcon = true;
					break;
				}

			}
		});

		mAnimatorSet = new AnimatorSet();

		float x = ViewHelper.getX(fab);
		ObjectAnimator translate = ObjectAnimator.ofFloat(fab, "translationX",
				x, x + 200, x);
		mAnimatorSet.playTogether(translate,
				ObjectAnimator.ofFloat(fab, "rotation", 0, 360, 360, 0));
		mAnimatorSet.setDuration(500);

		translate.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator arg0) {
				// TODO Auto-generated method stub
				if (arg0.getCurrentPlayTime() > 250 && changeIcon) {
					changeIcon = false;
					switch (indexFragment) {
					case INDEX_LIST_ALARM:
						fab.setImageResource(R.drawable.fab_add);
						break;

					case INDEX_SET_ALARM:
						fab.setImageResource(R.drawable.ic_widget_resume);
						break;
					}
				}
			}
		});

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub

		switch (indexFragment) {
		case INDEX_LIST_ALARM:
			exit();
			break;

		case INDEX_SET_ALARM:
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content, mListAlarmFragment).commit();
			indexFragment = INDEX_LIST_ALARM;
			break;
		}
	}

	private void exit() {
		if (doubleBackToExitPressedOnce) {

			// getSupportFragmentManager().beginTransaction()
			// .remove(mSetAlarmFragment).commit();

			super.onBackPressed();
			return;
		}

		this.doubleBackToExitPressedOnce = true;
		Toast.makeText(this, "Please click BACK again to exit",
				Toast.LENGTH_SHORT).show();

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				doubleBackToExitPressedOnce = false;
			}
		}, 2000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				fab.show(true);

			}
		}, 300);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		if (timeReceiver != null) {
			unregisterReceiver(timeReceiver);
			timeReceiver.removeListener(mTimeView);
			timeReceiver = null;
		}

	}
}
